package com.example.news.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.example.news.R;
import com.example.news.bean.TwoBean;

public class TwoAdapter extends BaseAdapter {

    private TwoBean twoBean;
    private Context context;
    TextView text;
    TextView top_comments_content;
    ImageView imageView;

    public TwoAdapter(TwoBean twoBean, Context context) {
        this.twoBean = twoBean;
        this.context = context;
    }

    @Override
    public int getCount() {
        return twoBean.getResult().size();
    }

    @Override
    public Object getItem(int i) {
        return twoBean.getResult().get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = LayoutInflater.from(context).inflate(R.layout.iteam,viewGroup,false);
        text=view.findViewById(R.id.text);
        top_comments_content=view.findViewById(R.id.name);
        imageView=view.findViewById(R.id.imageView);

        text.setText(twoBean.getResult().get(i).getText());
        top_comments_content.setText(twoBean.getResult().get(i).getTop_comments_content());
        Img(twoBean.getResult().get(i).getThumbnail());
        Log.e("22" +
                "",twoBean.getResult().get(i).getThumbnail());
     //  Img(twoBean.getResult().get(i).getTop_comments_header());

        return view;
    }

    public void Img(String a){

        RequestQueue requestQueue= Volley.newRequestQueue(context);

        ImageRequest imageRequest=new ImageRequest(a, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(context,"网络错误",Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(imageRequest);
    }
}
